Skip to content

Conversation

jschwartzentruber
Copy link
Contributor

Add test for #148278. This was written with the aide of ChatGPT 5 and tested on Linux x86_64.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Jesse Schwartzentruber (jschwartzentruber)

Changes

Add test for #148278. This was written with the aide of ChatGPT 5 and tested on Linux x86_64.


Full diff: https://github.com/llvm/llvm-project/pull/154190.diff

1 Files Affected:

  • (added) compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp (+73)
diff --git a/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp b/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp
new file mode 100644
index 0000000000000..7ed3ddd8b81ed
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp
@@ -0,0 +1,73 @@
+// This test case checks for a bug where inlined functions and anonymous code
+// result in out-of-order stack frame numbers.
+
+// UNSUPPORTED: android
+// UNSUPPORTED: aarch64
+// UNSUPPORTED: darwin
+// UNSUPPORTED: ios
+
+// RUN: %clangxx_asan -O0 -g %s -o %t
+// RUN: %env_asan_opts=symbolize=0 not %run %t DUMMY_ARG > %t.asan_report 2>&1
+// RUN: %asan_symbolize --log-level debug --log-dest %t_debug_log_output.txt -l %t.asan_report > %t.asan_report_sym
+// RUN: FileCheck --input-file=%t.asan_report_sym %s
+
+#include <sys/mman.h>
+#include <unistd.h>
+#include <cstdint>
+#include <cstring>
+#include <cstdio>
+#include <cstddef>
+
+static void call_via_anon_page(void (*fn)()) {
+  const size_t pagesz = static_cast<size_t>(sysconf(_SC_PAGESIZE));
+  uint8_t *mem = static_cast<uint8_t*>(
+      mmap(nullptr, pagesz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
+  if (mem == MAP_FAILED) perror("mmap");
+
+#if defined(__x86_64__)
+  // x86_64: mov rax, imm64; call rax; ret
+  // 48 B8 <imm64> FF D0 C3
+  uint8_t stub[2 + 8 + 2 + 1] = {0x48, 0xB8};
+  std::memcpy(stub + 2, &fn, sizeof(fn)); // imm64
+  stub[2 + 8] = 0xFF; stub[2 + 9] = 0xD0; // call rax
+  stub[2 + 10] = 0xC3;                    // ret
+#else
+#error "unsupported platform"
+#endif
+
+  std::memcpy(mem, stub, sizeof(stub));
+  mprotect(mem, pagesz, PROT_READ | PROT_EXEC);
+
+  using Thunk = void (*)();
+  reinterpret_cast<Thunk>(mem)();
+
+  munmap(mem, pagesz);
+}
+
+__attribute__((always_inline)) static void inline3(char* p, size_t n) {
+  // out-of-bounds write to trigger ASan
+  p[n] = 42;
+}
+
+__attribute__((always_inline)) static void inline2(char* p, size_t n) { inline3(p, n); }
+__attribute__((always_inline)) static void inline1(char* p, size_t n) { inline2(p, n); }
+
+__attribute__((noinline)) static void top() {
+  char p[8];
+  inline1(p, 16);  // deliberately OOB; inlined chain expands in symbolizer
+}
+
+int main() {
+  call_via_anon_page(top);
+  return 0;
+}
+
+// Check that the numbering of the stackframes is correct.
+
+// CHECK: AddressSanitizer: stack-buffer-overflow
+// CHECK-NEXT: WRITE of size
+// CHECK-NEXT: #0 0x{{[0-9a-fA-F]+}} in inline3
+// CHECK-NEXT: #1 0x{{[0-9a-fA-F]+}} in inline2
+// CHECK-NEXT: #2 0x{{[0-9a-fA-F]+}} in inline1
+// CHECK-NEXT: #3 0x{{[0-9a-fA-F]+}} in top
+// CHECK-NEXT: #4 0x{{[0-9a-fA-F]+}}

Copy link
Contributor

@thurstond thurstond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an XFAIL annotation (otherwise, this test will break the build as soon as it lands)

@thurstond
Copy link
Contributor

P.S. I think the JIT'ed code is the important part, the inlining just adds noise. When I remove the inlined functions, it still reproduces the wrong numbering:

#0 0x558d08aa220e in _ZL3topv compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp:49:11 
#1 0x7fc7968f700b (<unknown module>) 
#1 0x558d08aa204a in main compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp:53:3 
#2 0x7fc796343ca7 in __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16 
...

@jschwartzentruber jschwartzentruber force-pushed the add-asan-symbolize-unknown-frame-test branch 2 times, most recently from fb3ee4c to 1821833 Compare August 18, 2025 21:09
Copy link

github-actions bot commented Aug 18, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@jschwartzentruber jschwartzentruber force-pushed the add-asan-symbolize-unknown-frame-test branch from 1821833 to 6fd6f66 Compare August 18, 2025 21:17
Copy link
Contributor

@thurstond thurstond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch! Looks good to me.

Let me know if/when you'd like me to press the big merge button.

@jschwartzentruber
Copy link
Contributor Author

Thanks for the patch! Looks good to me.

Let me know if/when you'd like me to press the big merge button.

Thank you! Anytime is fine with me.

@thurstond thurstond merged commit 4d2288d into llvm:main Aug 18, 2025
9 checks passed
Copy link

@jschwartzentruber Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@jschwartzentruber jschwartzentruber deleted the add-asan-symbolize-unknown-frame-test branch August 19, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants